package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.query.EmployeeQueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper mapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Employee record) {
        return mapper.insert(record);
    }

    @Override
    public Employee selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Employee record) {
        return mapper.updateByPrimaryKey(record);
    }

    /**
     * 分页
     * @param qo
     * @return
     */
    @Override
    public PageInfo<Employee> listpage(EmployeeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> list = mapper.list(qo);
        return new PageInfo<Employee>(list);
    }

    @Override
    public Employee selectUser(String username, String password) {
        return mapper.selectUser(username,password);

    }

    @Override
    public void batchDelete(Long[] ids) {
        mapper.batchDelete(ids);
    }

    @Override
    public Employee selectByName(String name) {
        return mapper.selectByName(name);
    }

    //导出
    @Override
    public Workbook exportXls() {
        //创建excel
        Workbook workbook = new HSSFWorkbook();
        //创建sheet也就是一个表
        Sheet sheet = workbook.createSheet();
        //创建标题行
        Row row = sheet.createRow(0);
        //设置内容到单元格中
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");

        //查询数数据库所有的员工表
        List<Employee> list = mapper.selectAll();
        for (int i = 0; i < list.size(); i++) {
            //获取每一个员工
            Employee e = list.get(i);
            //创建下一个标题行
            row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(e.getName());
            row.createCell(1).setCellValue(e.getEmail());
            row.createCell(2).setCellValue(e.getAge());
        }
        return workbook;
    }

    //下载模板
    @Override
    public Workbook employeeImport() {
        //创建excel表
        Workbook workbook = new HSSFWorkbook();
        //创建sheet
        Sheet sheet = workbook.createSheet();
        //创建标题行
        Row row = sheet.createRow(0);
        //设置内容到单元格
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        return workbook;
    }

    //上传
    @Override
    public void importXls(MultipartFile file) throws IOException {
        //把接收到的文件以excel方式去读取操作
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        //读取第一页，也就是第一张表
        Sheet sheet = workbook.getSheetAt(0);
        //获取最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        //i要从1开始，原因是第一行是标题行
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            String email = row.getCell(1).getStringCellValue();
            int age = (int) row.getCell(2).getNumericCellValue();
            //判断如果有一个为空就不往下做
            if (!(StringUtils.hasLength(name.trim()) || StringUtils.hasLength(email.trim()))){
                return;
            }

            //不为空封装到employee中
            Employee employee = new Employee();
            employee.setName(name);
            employee.setEmail(email);
            employee.setAge(age);
            //设置默认密码为1,使用MD5方式加密
            Md5Hash hash = new Md5Hash("1",name);
            employee.setPassword(hash.toString());

            //添加到数据库
            try {
                mapper.insert(employee);
            } catch (Exception e) {
                e.printStackTrace();
                //因为数据库中name是唯一字段，不能重复，所以从excel表中取出来的如果重复执行下一条语句
                continue;
            }

        }

    }

    @Override
    public List<Employee> selectSeller(List<String> roleList) {
        return mapper.selectSeller(roleList);
    }
}
